package com.gzhryc.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;

import org.apache.commons.net.telnet.TelnetClient;

public class TelnetTools {

	private static final String ORIG_CODEC = "ISO8859-1";
	private static final String TRANSLATE_CODEC = "GBK";

	public static TelnetClientWrap getTelentClent(String hostName,
			Integer hostPort, String userName, String userPwd) {
		try {
			//TelnetClient telnet = new TelnetClient("VT100");// VT100 VT52 VT220 VTNT ANSI
			TelnetClient telnet = new TelnetClient();
			telnet.connect(hostName, hostPort);
			
			TelnetClientWrap wrap = new TelnetClientWrap(telnet);
			
			wrap.read("ogin: ");
			wrap.write(userName+"\r\n");
			wrap.read("assword: ");
			wrap.write(userPwd+"\r\n");
			wrap.read("# ");
			return wrap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static class TelnetClientWrap {

		TelnetClient telnet;
		InputStream in;
		PrintStream out;

		public TelnetClientWrap(TelnetClient telnet) {
			this.telnet = telnet;
			this.in = telnet.getInputStream();
			this.out = new PrintStream(telnet.getOutputStream());
		}

		public void read(String str) {
			try {
				StringBuffer sb = new StringBuffer();
				char c;
				int code = -1;
				boolean ansiControl = false;
				while ((code = (in.read())) != -1) {
					c = (char) code;
					if (c == '\033') {// vt100控制码都是以\033开头的。
						ansiControl = true;
						int code2 = in.read();
						char cc = (char) code2;
						if (cc == '[' || cc == '(') {
						}
					}
					if (!ansiControl) {
						if (c == '\r') {
							String olds = new String(sb.toString().getBytes(
									ORIG_CODEC), TRANSLATE_CODEC);
							System.out.println(olds);
							if (sb.lastIndexOf(str) != -1) {
								break;
							}
							sb.delete(0, sb.length());
						} else if (c == '\n')
							;
						else
							sb.append(c);
						if (sb.lastIndexOf(str) != -1) {
							break;
						}
					}

					if (ansiControl) {
						if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')
								|| c == '"') {
							ansiControl = false;
						}
					}
				}
				System.out.println(new String(sb.toString()
						.getBytes(ORIG_CODEC), TRANSLATE_CODEC));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		public void write(String s) {  
	        try {  
	            out.write(s.getBytes());  
	            out.flush();  
	            System.out.println(s);  
	        } catch (Exception e) {  
	        }  
	    }  

		public void close() {
			if (out != null){
				out.close();
			}
			if (in != null){
				try {
					in.close();
				} catch (IOException e1) {
				}
			}
			if (telnet != null){
				try {
					telnet.disconnect();
				} catch (IOException e) {
				}
			}
		}

		public TelnetClient getTelnet() {
			return telnet;
		}

		public void setTelnet(TelnetClient telnet) {
			this.telnet = telnet;
		}
	}
}
